﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Web.Configuration;
using Evaluation3.data;


public partial class WebUserControl_Nhap_ChiTietThongTin : System.Web.UI.UserControl
{
    ChiTietThongTin chiTietThongTin;
    DataRow dr;

    SanPham objSanPham;
    HoiDong objHoiDong;

    OleDbConnection myConnection;
    protected void Page_Load(object sender, EventArgs e)
    {
       
            String connectinString = WebConfigurationManager.ConnectionStrings["Evaluation"].ConnectionString;
            myConnection = new OleDbConnection(connectinString);
            //String strMaChiTietThongTin = Request.Params["MaChiTietThongTin"].ToString();
            //txtMaChiTietThongTin.Text = strMaChiTietThongTin;
            try
            {
                myConnection.Open();
                chiTietThongTin = new ChiTietThongTin();
                chiTietThongTin.ActiveConnection = myConnection;
                    //chiTietThongTin.SelectCondition = "MaChiTietThongTin=" + strMaChiTietThongTin;
                chiTietThongTin.LoadData();
                if (!IsPostBack)
                {
                   

                    objSanPham = new SanPham();
                    objSanPham.ActiveConnection = myConnection;
                    //objSanPham.SelectCondition = "MaChiTietThongTin=" + strMaChiTietThongTin;
                    objSanPham.LoadData();

                    DDList_TenSanPham.DataSource = objSanPham;
                    DDList_TenSanPham.DataTextField = "TenSanPham";
                    DDList_TenSanPham.DataValueField = "MaSanPham";
                    DDList_TenSanPham.DataBind();

                   

                    //dr = chiTietThongTin.Rows[0];
                }
                objHoiDong = new HoiDong();
                objHoiDong.ActiveConnection = myConnection;
                //objHoiDong.SelectCondition = "MaHoiDong=0";
                objHoiDong.LoadData();
                DDList_TenHoiDong.DataSource = objHoiDong;
                DDList_TenHoiDong.DataValueField = "MaHoiDong";
                DDList_TenHoiDong.DataTextField = "TenHoiDong";
                DDList_TenHoiDong.DataBind();
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            finally
            {
                myConnection.Close();
            }
       
    }
    protected void bntCapNhat_Click(object sender, EventArgs e)
    {

        if (DDList_TenSanPham.Text == null || DDList_TenSanPham.SelectedValue.Trim().Length < 1)
        {
            lblTenSanPham.Text = "* Tên sản phẩm không được để trống!";
            lblTenSanPham.Visible = true;
            DDList_TenSanPham.Focus();
            return;
        }
        else
        {
            lblTenSanPham.Visible = false;
        }

        if (DDList_TenHoiDong.Text == null || DDList_TenHoiDong.SelectedValue.Trim().Length < 1)
        {
            lblTenHoiDong.Text = "* Tên hội đồng không được để trống!";
            lblTenHoiDong.Visible = true;
            DDList_TenHoiDong.Focus();
            return;
        }
        else
        {
            lblTenHoiDong.Visible = false;
        }

        if (txtTenChiTietThongTin.Text == null || txtTenChiTietThongTin.Text.Trim().Length < 1)
        {
            lblTen.Text = "* Tên chi tiết thông tin không được để trống!";
            lblTen.Visible = true;
            txtTenChiTietThongTin.Focus();
            return;
        }
        else
        {
            lblTen.Visible = false;
        }
        if (txtAlphaI.Text != null)
        {
            try
            {
                double I = 0;
                I = Convert.ToDouble(txtAlphaI.Text);
                if (I < 0 || I > 1)
                {
                    lblAlphaI.Text = "* Điểm Alpha I phải trong khoảng từ 0 đến 1!";
                    lblAlphaI.Visible = true;
                    txtAlphaI.Focus();
                    return;
                }
                else
                {
                    lblAlphaI.Visible = false;
                }
            }
            catch (Exception eConvert)
            {
                lblAlphaI.Text = "* Điểm Alpha I phải là kiểu số!";
                lblAlphaI.Visible = true;
                txtAlphaI.Focus();
                Console.WriteLine(eConvert.Message);
                return;

            }

        }
        else if (txtAlphaI.Text == null)
        {
            lblAlphaI.Text = "* Điểm Alpha I không được để trống!";
            lblAlphaI.Visible = true;
            txtAlphaI.Focus();
            return;
        }
        else
        {
            lblAlphaI.Visible = false;
        }
        //
        dr = chiTietThongTin.NewRow();
        dr["MaChiTietThongTin"] = txt_MaChiTietThongTin.Text;
        dr["TenChiTietThongTin"] = txtTenChiTietThongTin.Text;
        dr["MaSanPham"] = DDList_TenSanPham.SelectedValue;
        dr["MaHoiDong"] = txt_MaChiTietThongTin.Text;
        dr["AlphaI"] = txtAlphaI.Text;
        dr["GhiChu"] = txtGhiChu.Text;
        chiTietThongTin.Rows.Add(dr);
        //chiTietThongTin.SaveData(EVALUATIONIII_COMMON.dataCommon.MyDataTable.DataTableAction.AddNew);

    }

    private void FillDropDown()
    { 
        
    }

    protected void DDList_TenSanPham_SelectedIndexChanged(object sender, EventArgs e)
    {
        //try   
        //{
        objHoiDong.DefaultView.RowFilter = "MaHoiDong=" +DDList_TenSanPham.SelectedValue;
        DDList_TenHoiDong.DataBind();
        txt_MaChiTietThongTin.Text = DDList_TenHoiDong.SelectedValue.ToString();
        //}
        //catch (Exception fdf)
        //{ 
        
        //}
        
    }
}
